package com.example.lesson

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.core.base.BaseActivity
import com.example.lesson.databinding.ActivityLessonBinding
import com.example.lesson.entity.Lesson

class LessonActivity : BaseActivity<ActivityLessonBinding,LessonPresenter>() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun initViewBinding() = ActivityLessonBinding.inflate(layoutInflater)

    override fun initData() {
        presenter.fetchData()
    }

    override fun initListener() {
    }

    private val lessonAdapter by lazy {
        LessonAdapter()
    }
    override fun initView() {
        viewBinding.apply {
            toolbar.inflateMenu(R.menu.menu_lesson)
            toolbar.setOnMenuItemClickListener {
                presenter.showPlayback()
                false
            }

            list.layoutManager = LinearLayoutManager(this@LessonActivity)
            list.adapter = lessonAdapter
            swipeRefreshLayout.setOnRefreshListener {
                presenter.fetchData()
            }
            swipeRefreshLayout.isRefreshing = true
        }
    }

    fun showLesson(lessons: List<Lesson>) {
        Log.d(TAG, "showLesson: $lessons")
        lessonAdapter.updateAndNotify(lessons)
        closeRefresh()
    }

    fun closeRefresh() {
        viewBinding.swipeRefreshLayout.isRefreshing = false
    }

    override fun initPresenter(): LessonPresenter = LessonPresenter(this)
}